package it.tukano.treditor.engine.geom;

import it.tukano.treditor.engine.mesh.Mesh3D;
import java.util.ArrayList;

public class GridBuilder {

    public static GridBuilder newInstance() {
        return new GridBuilder();
    }

    /**
     * Instance initializer
     */
    public GridBuilder() {
    }
    
    public Mesh3D generateXYGrid(float cellSize, int lineCount) {
        float gridSize = cellSize * lineCount;
        float min = -gridSize / 2;
        float max = gridSize / 2;
        ArrayList<NSegment> segments = new ArrayList<>();
        for(int i = 0; i < lineCount; i++) {
            float offset = min + cellSize * i;
            NSegment seg0 = new NSegment(new NTuple3(offset, min, 0), new NTuple3(offset, max, 0));
            NSegment seg1 = new NSegment(new NTuple3(min, offset, 0), new NTuple3(max, offset, 0));
            segments.add(seg0);
            segments.add(seg1);
        }
        Mesh3D mesh = new Mesh3D(null, segments.<NSegment>toArray(new NSegment[segments.size()]), new NTuple3(1, 0, 0));
        return mesh;
    }
    
    public Mesh3D generateXZGrid(float cellSize, int lineCount) {
        float gridSize = cellSize * lineCount;
        float min = -gridSize / 2;
        float max = gridSize / 2;
        ArrayList<NSegment> segments = new ArrayList<>();
        for(int i = 0; i < lineCount; i++) {
            float offset = min + cellSize * i;
            NSegment seg0 = new NSegment(new NTuple3(offset, 0, min), new NTuple3(offset, 0, max));
            NSegment seg1 = new NSegment(new NTuple3(min, 0, offset), new NTuple3(max, 0, offset));
            segments.add(seg0);
            segments.add(seg1);
        }
        Mesh3D mesh = new Mesh3D(null, segments.<NSegment>toArray(new NSegment[segments.size()]), new NTuple3(0, 1, 0));
        return mesh;
    }
    
    public Mesh3D generateYZGrid(float cellSize, int lineCount) {
        float gridSize = cellSize * lineCount;
        float min = -gridSize / 2;
        float max = gridSize / 2;
        ArrayList<NSegment> segments = new ArrayList<>();
        for(int i = 0; i < lineCount; i++) {
            float offset = min + cellSize * i;
            NSegment seg0 = new NSegment(new NTuple3(0, min, offset), new NTuple3(0, max, offset));
            NSegment seg1 = new NSegment(new NTuple3(0, offset, min), new NTuple3(0, offset, max));
            segments.add(seg0);
            segments.add(seg1);
        }
        Mesh3D mesh = new Mesh3D(null, segments.<NSegment>toArray(new NSegment[segments.size()]), new NTuple3(0, 0, 1));
        return mesh;
    }
}
